/*实用类*/
#ifndef __MY_UTIL_H__
#define __MY_UTIL_H__

#include <iostream>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
namespace ltylog
{
    namespace util
    {
        class Date
        {
        public:
            static size_t now()
            {
                return (size_t)time(nullptr);
            }
        };

        class File
        {
        public:
            static bool exists(const std::string &pathname)
            {
                struct stat st;
                if(stat(pathname.c_str(),&st)<0) return false;//检查文件信息是否存在 windows&linux
//                return (access(pathname.c_str(),F_OK)==0);//linux检查文件信息是否存在
                return true;
            }
            static std::string path(const std::string &pathname)
            {//./aaa/bbb/c.txt
                size_t pos=pathname.find_last_of("/\\");
                if(pos==std::string::npos) return ".";
                return pathname.substr(0,pos+1);
            }
            static void createDirectory(const std::string &pathname)
            {
                //./aaa/bbb/c.txt
                size_t pos=0,idx=0;
                while(idx<pathname.size())
                {
                    pos=pathname.find_first_of("/\\",idx);//从idx位置开始找
                    if(pos==std::string::npos)
                    {
                        mkdir(pathname.c_str(),0777);
                    }
                    std::string parent_dir=pathname.substr(0,pos+1);
                    if(exists(parent_dir)){ idx=pos+1; continue; }
                    mkdir(parent_dir.c_str(),0777);//一路下来没有continue就直接创建
                    idx=pos+1;
                }

            }
        };
    }
}
#endif